package com.chat.login.common.validation;

import com.chat.login.annotate.ValidAccount;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.stereotype.Component;

@Component
public class AccountValidator implements ConstraintValidator<ValidAccount, String> {
    private int min;
    private int max;

    @Override
    public void initialize(ValidAccount constraintAnnotation) {
        min = constraintAnnotation.min();
        max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null || value.isEmpty()) {
            return true;
        }
        if (!value.matches("\\d*")) {
            return false;
        }
        return value.length() >= min && value.length() <= max;
    }
}